<?php
    /**
     * GameServer
     *
     * The GameServer class provides managing games and game servers
     *
     * @copyright Copyright (c) 2011,  Progect
     */
     
    /**
     * xDataBase
     *
     * The xDataBase class provides connection and using data base
     *
     * @copyright Copyridfdfd, xEngine Progect
     */
    class GameServer  implements iJSingleton{
       
       
        /**
         *
         * This function is class construct
         *
         * @access public
         */  
        public function __construct() {

        }
        
        
    /**
     *
     * Return list of games
     * 
     * @param int    $page   - page number
     * @param string $search - search string 
     * @param int    $limt   - result limit
     * 
     * @return game list
     * @access public
     */
    public function getGamesList($page, $search, $limit = '') {
      $iJDataBase  =  iJFactory::singleton('iJDataBase');
      $QGames    =  $iJDataBase->query('SELECT 
                                          id, 
                                          code,
                                          title 
                                        FROM :table_games
                                        ');
      $QGames->bindTable(':table_games', TABLE_GAMES);
      
      if(!empty($search)) {
        $QGames->appendQuery('WHERE code like :search OR title like :search');
        $QGames->bindValue(':search', '%' . $search . '%');
      }
      
      $QGames->appendQuery(':limit');
      $QGames->bindTable(':limit', $QGames->addLimit($page, $limit));
      $QGames->execute();
      
      $games = array();
      
      while($QGames->next()) {
        $game['id']    = $QGames->value('id');
        $game['code']  = $QGames->value('code');
        $game['title'] = $QGames->value('title');
        $game['trash'] = iJHrefLink::linkToLocalPage('admin_games') . '?delKey=' . $QGames->value('code');
        $games[] = $game;
      }
      
      return array($games, $QGames);
      
    }
    
    
    
    /**
     *
     * Return game server's list
     * 
     * @param int    $page   - page number
     * @param string $search - search string 
     * @param string $type   - game server type 
     * @param string $search - game servr rankID 
     * @param int    $limt   - result limit
     * 
     * @return game list
     * @access public
     */
    public function getGameServersList($page, $search, $type = '', $rank = 0, $status = 0, $limit = '') {
      $iJDataBase  =  iJFactory::singleton('iJDataBase');
      $QServer     =  $iJDataBase->query('SELECT 
                                          id, 
                                          title,
                                          url,
                                          fileUrl,
                                          type, 
                                          rank,
                                          status,
                                          UNIX_TIMESTAMP(added) as added,
                                          UNIX_TIMESTAMP(updated) as updated
                                        FROM :table_game_servers
                                        ');
      $QServer->bindTable(':table_game_servers', TABLE_GAME_SERVERS);
      $and = false;
      
      if(!empty($search)) {
        $QServer->appendQuery('WHERE title like :search');
        $QServer->bindValue(':search', '%' . $search . '%');
        $and = true;
      }
      
      if($rank != 0) {
        if($and) {
          $QServer->appendQuery('AND rank = :rank');
        } else {
          $QServer->appendQuery('WHERE rank = :rank');
          $and = true;
        }
        $QServer->bindValue(':rank', $rank);
      } 
      
      if($status != 0) {
        if($and) {
          $QServer->appendQuery('AND status = :status');
        } else {
          $QServer->appendQuery('WHERE status = :status');
          $and = true;
        }
        $QServer->bindValue(':status', $status);
      }
      if($type != '') {
        if($and) {
          $QServer->appendQuery('AND type like :type');
        } else {
          $QServer->appendQuery('WHERE type like :type');
        }
        $QServer->bindValue(':type', '%' . $type . '%');
      }
      
      $QServer->appendQuery(':limit');
      $QServer->bindTable(':limit', $QServer->addLimit($page, $limit));
      $QServer->execute();
      
     // spr($QServer);
      $servers = array();
      while($QServer->next()) {
        $server['id']      = $QServer->value('id');
        $server['title']   = $QServer->value('title');
        $server['url']     = $QServer->value('url');
        $server['file']    = $QServer->value('fileUrl');
        $server['type']    = $QServer->value('type');
        $server['rank']    = $QServer->value('rank');
        $server['status']  = $QServer->value('status');
        $server['added']   = date ("G:i j M Y", $QServer->value('added'));
        $server['updated'] = date ("G:i j M Y", $QServer->value('updated'));
        $server['trash']   = iJHrefLink::linkToLocalPage('admin_games') . '?delKey=' . $QServer->value('code');
        $servers[] = $server;
      }
      
      return array($servers, $QServer);
      
    }
    
    /**
     * 
     * function return rank's list
     * 
     * @return ranks list
     * @access public
     */
    public function getGameServerRanks() {
       $iJDataBase  =  iJFactory::singleton('iJDataBase');
       $QRank       =  $iJDataBase->query('SELECT 
                                          id, 
                                          code
                                        FROM :table_rank
                                        ');
       $QRank->bindTable(':table_rank', TABLE_RANK);
       $QRank->execute();
       
       $ranks = array();
       while($QRank->next()) {
          $rank['id']   = $QRank->value('id');
          $rank['code'] = $QRank->value('code');
          $ranks[] = $rank;
       }
       return $ranks;
    }
    
    
    /**
     * 
     * function return rank's list
     * 
     * @return ranks list
     * @access public
     */
    public function getGameServerStatuses() {
       $iJDataBase  =  iJFactory::singleton('iJDataBase');
       $QStatus      =  $iJDataBase->query('SELECT 
                                          id, 
                                          code
                                        FROM :table_status
                                        ');
       $QStatus->bindTable(':table_status', TABLE_STATUS);
       $QStatus->execute();
       
       $statses = array();
       while($QStatus->next()) {
          $status['id']   = $QStatus->value('id');
          $status['code'] = $QStatus->value('code');
          $statses[] = $status;
       }
       return $statses;
    }
    
    
    /**
     * 
     * function return rank's list
     * 
     * @return ranks list
     * @access public
     */
    public function getGameServerGameTypes() {
       $iJDataBase  =  iJFactory::singleton('iJDataBase');
       $QStatus     =  $iJDataBase->query('SELECT 
                                          id, 
                                          code
                                        FROM :table_games
                                        ');
       $QStatus->bindTable(':table_games', TABLE_GAMES);
       $QStatus->execute();
       
       $statses = array();
       while($QStatus->next()) {
          $status['id']   = $QStatus->value('id');
          $status['code'] = $QStatus->value('code');
          $statses[] = $status;
       }
       return $statses;
    }
    
    public function getServerChilds($serverID, $limit = '') {
      $iJDataBase = iJFactory::singleton('iJDataBase');
      $QChild     =  $iJDataBase->query('SELECT 
                                          rates, 
                                          head,
                                          info
                                        FROM :table_server_childs
                                        ');
      $QChild->bindTable(':table_server_childs', TABLE_SERVER_CHILDS);
      $childs = array();
      while($QStatus->next()) {
         $child['rates']   = $QChild->value('rates');
         $child['head']    = $QChild->value('head');
         $child['info']    = $QChild->value('info');
         $childs[] = $child;
      }
       
      $QChild->execute();
      
      return array($childs, $QChild);
      
    }
    
        
        
        
        
 
    }
?>